package net.valyrian.game.pantallaInicio;

import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
//import com.badlogic.gdx.controllers.Controllers;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class MainMenu implements Screen {

	private TextureAtlas atlas; //done
	private Stage stage;
	private Skin skin; //done
	private Table table;
	private Image background;
	private Music music;
	//private Sprite bgSprite;
	//private Texture bgtexture;
	//private BitmapFont white, black; //done
	//private Controllers controller;
	private TweenManager tweenManager;
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		
		stage.act(delta);
		
		stage.draw();
		tweenManager.update(delta);

	}

	@Override
	public void resize(int width, int height) {		
	//	show();
		stage.setViewport(width, height, true);
		table.invalidateHierarchy();
		background.setSize(width, height);
	}

	@Override
	public void show() {
		//OJO!!!
		//Para saber si se esta corriendo en desktop, andrid, etc:
		//if(Gdx.app.getType() == ApplicationType.Desktop)
		//
		
		
		/*ESTO ES SE AGREGO AL ARCHIVO JSON 
		 * PENDIENTE DE MODIFICAR ESTA LINEA!!!
		 * LabelStyle hs = new LabelStyle(white, Color.WHITE);
		 */

		music = Gdx.audio.newMusic(Gdx.files.internal("audio/title_song.mp3"));
			
//		Joystick js = new Joystick();
//		controller = new Controllers();
//		controller.addListener(js);
		
		stage = new Stage();
		Gdx.input.setInputProcessor(stage);
		
		//Cargando la imagen de fondo del menu
		background = new Image(new Texture(Gdx.files.internal("img/title_background.png")));
		
		
//		Group background = new Group();
//		Texture bgtexture = new Texture(Gdx.files.internal("img/title_background.png"));
//		//bgSprite = new Sprite(bgtexture);
//		background.addActor(new Image(bgtexture ));
//		//background.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//		background.size(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//		//background.scale(bgtexture.getWidth()/Gdx.graphics.getWidth(), bgtexture.getHeight()/Gdx.graphics.getHeight());
		
		atlas = new TextureAtlas("ui/atlas.pack");
		
		skin = new Skin(Gdx.files.internal("ui/menuSkin.json"),atlas);
		table = new Table(skin);
		table.setFillParent(true);
	//	table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		
		Label heading = new Label("MISTY MOUNTAIN MISTERY", skin);
		heading.setFontScale(1.5f);
/*
 * ESTO SE AGREGO AL ARCHIVO JSON
 
		//Definicion de las fuentes
		white = new BitmapFont(Gdx.files.internal("fonts/font_white.fnt"));
		black = new BitmapFont(Gdx.files.internal("fonts/font_black.fnt"));
*/		
		
		/*ESTO HA SIDO AGREGADO A LOS ARCHIVOS JSON PARA REDUCIR CODIGO*/
		//Deficion de los estilos de los botones
		
//		TextButtonStyle tbs = new TextButtonStyle();
//		tbs.up = skin.getDrawable("button.up");
//		tbs.down = skin.getDrawable("button.down");
//		tbs.pressedOffsetX = 0;
//		tbs.pressedOffsetY = -5;
//		tbs.font = black;
		
		
		TextButton 	buttonPlay = new TextButton("JUGAR", skin);
		buttonPlay.padBottom(20);
		buttonPlay.padLeft(10);
		buttonPlay.padRight(10);
		buttonPlay.padTop(10);
		buttonPlay.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
		((Game) Gdx.app.getApplicationListener()).setScreen(new Levels());			
			}
			
		});
		
		/*Esta linea es para cuando no se trabaja con el JSON, y se usa un TextButtonStyle*/
		//buttonExit = new TextButton("SALIR DEL JUEGO", tbs);
		TextButton 	buttonExit = new TextButton("SALIR DEL JUEGO",skin);
		buttonExit.padBottom(20);
		buttonExit.padLeft(10);
		buttonExit.padRight(10);
		buttonExit.padTop(10);
		buttonExit.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Gdx.app.exit();
			}
			
		});
		
		
		
		
		/*Se agregan los elementos a la tabla y luego la tabla al stage*/
		//table.add(background);
		table.add(heading);
		table.getCell(heading).spaceBottom(200).spaceTop(200);
		table.row();
		table.add(buttonPlay);
		table.getCell(buttonPlay).spaceBottom(50);
		table.row();
		table.add(buttonExit);
		background.getColor().a = .7f;
		stage.addActor(background);
		stage.addActor(table);
		
		
		/*Creando animaciones*************************************/
		
		tweenManager = new TweenManager();
		Tween.registerAccessor(Actor.class, new ActorAccessor());
		
		/*Timeline para el efecto de cambio de colores del titulo*/
		Timeline.createSequence().beginSequence()
		.push(Tween.to(heading, ActorAccessor.RGB, .5f).target(0, 0, 1))
		.push(Tween.to(heading, ActorAccessor.RGB, .5f).target(0, 1, 0))
		.push(Tween.to(heading, ActorAccessor.RGB, .5f).target(1, 0, 0))
		.push(Tween.to(heading, ActorAccessor.RGB, .5f).target(1, 1, 0))
		.end().repeat(Tween.INFINITY, 0).start(tweenManager);
		
		/*Timeline para el efecto de desvanecimiento de botones y titulo*/
		Timeline.createSequence().beginSequence()
		.push(Tween.set(buttonPlay, ActorAccessor.ALPHA).target(0))
		.push(Tween.set(buttonExit, ActorAccessor.ALPHA).target(0))
		.push(Tween.from(heading,ActorAccessor.ALPHA ,1.5f).target(0))
		.push(Tween.to(buttonPlay, ActorAccessor.ALPHA ,.5f).target(1))
		.push(Tween.to(buttonExit, ActorAccessor.ALPHA ,.5f).target(1))
		.end().start(tweenManager);
	
		/*Efecto de "llegada" de la tabla*/
		Tween.from(table, ActorAccessor.ALPHA, .5f).target(0).start(tweenManager);
		Tween.from(table, ActorAccessor.Y,.5f).target(Gdx.graphics.getHeight()/8).start(tweenManager);
	
		tweenManager.update(Gdx.graphics.getDeltaTime());
		music.play();
		music.setVolume(.8f);
		music.setLooping(true);
	}

	@Override
	public void hide() {
		dispose();

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		stage.dispose();
		atlas.dispose();
		skin.dispose();
		music.dispose();
//		white.dispose();
//		black.dispose();
	}
	
}
